package name.huzhenbo.java.enums;

import java.util.HashMap;
import java.util.Map;

/**
 * associate operation to Enum.
 */
public enum Operation {
    PLUS("+") {public float apply(float x, float y) {
        return x + y;
    }},
    MINUS("-") {public float apply(float x, float y) {
        return x - y;
    }},
    MULTIPLY("*") {public float apply(float x, float y) {
        return x * y;
    }},
    DIVIDE("/") {public float apply(float x, float y) {
        return x / y;
    }};
    private final String symbol;
    private static Map<String, Operation> stringToEnum = new HashMap<String, Operation>();

    static {
        for (Operation operation : values()) {
            stringToEnum.put(operation.symbol, operation);
        }
    }

    public abstract float apply(float x, float y);

    Operation(String symbol) {
        this.symbol = symbol;
    }

    @Override
    public String toString() {
        return symbol;
    }

    public static Operation fromString(String symbol) {
        return stringToEnum.get(symbol);
    }
}
